﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Trippy.CollidableGameComponents;
using Trippy.CollidableGameComponents.Agents;
using Trippy.Levels;
using Trippy.CollidableGameComponents.Projectiles;
using Trippy.Animation;

namespace Trippy.Abilities
{
    class Firestorm : Ability
    {
        EffectAnimation Channel;

        public Firestorm(Level level) : base (level)
        {
            name = "Firestorm";
            icon = CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Ability Icons/fire");
        }

        public override void OnUpdate(float elapsedTime)
        {
            if (State == AbilityState.Active)
            {
                Random r = new Random();

                if (r.Next(0, 9) == 0)
                {
                    double angle = ((double)r.Next(0, 20)) * (Math.PI / 20) + Math.PI;
                    Fireball f = new Fireball(CurrentLevel);

                    f.Position = new Vector2(CurrentLevel.Hero.Position.X + (Convert.ToSingle(Math.Cos(angle)) * 80f),
                        CurrentLevel.Hero.Position.Y + (Convert.ToSingle(Math.Sin(angle)) * 80f));
                    f.Velocity = new Vector2(Convert.ToSingle(Math.Cos(angle)),
                        Convert.ToSingle(Math.Sin(angle))) * 200f;
                    f.Acceleration = CurrentLevel.Hero.Acceleration;
                    f.MarkForAddition();
                }
            }
            base.OnUpdate(elapsedTime);
        }

        public override void OnLand()
        {
            if(!timerActive)
                State = AbilityState.Available;
            base.OnLand();
        }

        public override void OnAir()
        {
            State = AbilityState.Unavailable;
            OnCancel();
            base.OnAir();
        }
        
        public override void OnActivate()
        {
            State = AbilityState.Active;
            CurrentLevel.Hero.Velocity = new Vector2();
            SetTimer(10f);

            Channel = new EffectAnimation(CurrentLevel,
                CurrentLevel.Game.Content.Load<Texture2D>("Sprites/Effects/castfire"),
                64, 64, 0, 0, 0, 0, 15, 0.15f, 20, CurrentLevel.Hero.Position);

            base.OnActivate();
        }

        public override void OnMoveLeft(float elapsedTime)
        {
            OnCancel();
            base.OnMoveLeft(elapsedTime);
        }

        public override void OnMoveRight(float elapsedTime)
        {
            OnCancel();
            base.OnMoveRight(elapsedTime);
        }

        public override void OnJump()
        {
            OnCancel();
            base.OnJump();
        }

        public override void OnRemove()
        {
            OnCancel();
            base.OnRemove();
        }

        public override void OnCancel()
        {
            if (State == AbilityState.Active)
            {
                State = AbilityState.Unavailable;
                SetTimer(30f);
                Channel.StopAnimation();
            }
            base.OnCancel();
        }

        protected override void OnTimer()
        {
            if (State == AbilityState.Active)
            {
                OnCancel();
            }
            else if (State == AbilityState.Unavailable)
                State = AbilityState.Available;
        }
    }
}
